package cn.jdy.audit.admin;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.audit.entity.EmailRecord;
import cn.jdy.audit.service.EmailRecordService;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.platform.service.ConfService;


/**
 * @title 大数据email统计后台操作
 * @author jga
 * @since 1-11-2016
 */
@Controller
@RequestMapping("/emailRecord")
public class EmailRecordCtrl extends BaseCtrl{
  
	@Autowired
	private EmailRecordService emailRecordService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/EmailRecord/list?s=";
	
	/**
	 *  初始化
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(EmailRecord entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/audit/emailRecord/emailRecordList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
	  this.emailRecordService.updateSql();
    mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(EmailRecord sp){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", emailRecordService.query(sp));
			jsonMap.put("iTotalDisplayRecords", emailRecordService.count(sp));
			jsonMap.put("iTotalRecords", emailRecordService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView("/EmailRecord/EmailRecordDetail.jsp");
		System.out.println(this.confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L).size());
		mv.addObject("column", this.confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		mv.addObject("data", id==null||id==0 ? id : this.emailRecordService.find(id));
		return mv;
	}
	
	/**
	 * 保存信息
	 * @param sp
	 * @param pid
	 * @return
	 */
	@RequestMapping("/save")
	 @ResponseBody
	public Map<String, Object> save(EmailRecord sp, String file) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		if(sp.getId()==null) {//
				sp.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.emailRecordService.save(sp);
				mv.setViewName(this.viewName.concat("1"));
			} else {//更新
				this.emailRecordService.update(sp);
				mv.setViewName(this.viewName.concat("2"));
			}
    jsonMap.put("statusCode", 200);
		} catch(Exception e) {
      mv.addObject("statusCode", "500");
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "信息保存异常！", e);
		}

	   return jsonMap;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.emailRecordService.delete(id);
			 mv.setViewName(this.viewName.concat("3"));
	      // 清理内容页缓存
	      NginxPurgeCacheUtil.purge(4, id);
		} catch(Exception e) {
	     mv.setViewName(this.viewName.concat("4"));
	      BaseLog.e(getClass(), "删除异常！", e);
		}
		return mv;
	}
	
	 /**
   * 统计
   * @return
   */
  @RequestMapping("/emailRecordtoCount")
  public ModelAndView emailRecordtoCount(EmailRecord sp) {
    ModelAndView mv = new ModelAndView("/audit/emailRecord/emailRecordCount.jsp");
    return mv;
  }
  

	  @RequestMapping("/emailRecordCount")
	  @ResponseBody
	  public Map<String, Object> emailRecordCount(EmailRecord sp) {
	    Map<String, Object> jsonMap = new HashMap<String, Object>();
	    jsonMap.put("classList", emailRecordService.emailRecordCount(sp));
	    jsonMap.put("entity", sp);
	    return jsonMap;
	  }
	   /**
	    * 下载订单
	    *
	    * @return
	    */
	   @RequestMapping("/downExcel")
	   public void downExcel(EmailRecord order, String fromDate, String toDate, HttpServletResponse response) {
	     List<EmailRecord> list = this.emailRecordService.query(order);
	     String[] titles = new String[]{"id", "ip", "email标题", "email内容", "src内容", "dest内容", "添加时间"};
	     String[][] fieldNames = new String[][]{{"id"}, {"user_ip"}, {"email_title"},{"email_content"}, {"src_account"}, {"dest_account"}, {"createTime"}};
	     super.downExcel(list, titles, fieldNames, "email信息", response);

	   }
}
